import math;
import pymongo
import xlrd
import datetime
import time
import json
import math
table = 'main_kongqi'
def handle_queshi(data):
    for i in range(0,len(data)):
        for dicts in data[i]:
            if(i<=5):
                if data[i][dicts] == '':
                    handledata = []
                    handle = data[0:i]
                    handle2 = data[i+1:i+4]
                    h = handle + handle2
                    for j in h:
                        handledata.append(float(j[dicts]))
                    data[i][dicts] = math.ceil(float(sum(handledata)/len(handledata))*10000)/10000
            elif(i>5 and i<=len(data)-5):
                if data[i][dicts] == '':
                    handledata = []
                    handle = data[i-5:i]
                    handle2 = data[i+1:i+4]
                    h = handle + handle2
                    for j in h:
                        handledata.append(float(j[dicts]))
                    data[i][dicts] = math.ceil(float(sum(handledata)/len(handledata))*10000)/10000
            else:
                if data[i][dicts] == '':
                    handledata = []
                    handle = data[i-8:i]
                    h = handle + handle2
                    for j in h:
                        handledata.append(float(j[dicts]))
                    data[i][dicts] = math.ceil(float(sum(handledata)/len(handledata))*10000)/10000
    # with open('./test_text.txt','w') as file_object:
    #     file_object.write(str(data))
    return data

def update(title,name,handle_data):
    myclient = pymongo.MongoClient("mongodb://localhost:27017/")
    mydb = myclient[table]
    mycol = mydb[title]
    x = mycol.find_one()
    data_hours = x['data_hours']
    
    with open('./test_text1.txt','w') as file_object:
        file_object.write(str(data_hours))
    for i in data_hours:
        if i['sheet'] == name:
            i['data'] = handle_data
    # x['data_hour'] = data_hours
    myquery = {"title":x['title']}
    newvalues = {"$set":{"data_hours":data_hours}}
    mycol.update_one(myquery, newvalues)
    return 1
